<?php

class neogeoidx_city extends XoopsObject{
	function neogeoidx_city (){
	$this->XoopsObject();
	$this->initVar("id",XOBJ_DTYPE_INT,0,false,5);
	$this->vars['id']['primary'] = true;
	$this->initVar('country_id', XOBJ_DTYPE_INT, 0, false,4);								
	$this->initVar('country_title', XOBJ_DTYPE_TXTBOX, 0, false,255);									
	$this->initVar('region_id', XOBJ_DTYPE_INT, 0, false,4);			
	$this->initVar('region_title', XOBJ_DTYPE_TXTBOX, 0, false,255);				
	$this->initVar('city_id', XOBJ_DTYPE_INT, 0, false,4);			
	$this->initVar('city_title', XOBJ_DTYPE_TXTBOX, 0, false,255);				
	}
}

class neogeoidx_cityHandler extends XoopsObjectHandler {
	var $table="neogeoidx_city";
	  function &create($isNew = true)
    {
        $item = new neogeoidx_city();
        if ($isNew) {
            $item->setNew();
        }
        return $item;
    }
	function createtable(&$item){
		$sql = "DROP TABLE ".$this->db->prefix($this->table)." ";
#		echo $sql;
		$this->db->queryF($sql);
		echo $this->db->Error();
		$sqltxt = "CREATE TABLE ".$this->db->prefix($this->table)." (";	
		$fields = array();
		$keys = array();
		foreach ($item->vars as $key=>$arrval){
			 $keys[] = $key;
			switch($arrval['data_type']){
			case XOBJ_DTYPE_INT:
					if ($item->vars[$key]['primary']) {$fields[] =  "$key INT($arrval[maxlength]) NOT NULL auto_increment PRIMARY KEY";}
			 		 else {$fields[] =  "$key INT($arrval[maxlength]) NOT NULL";}
					break;
			case XOBJ_DTYPE_TXTBOX:
			 		$fields[] =  "$key VARCHAR($arrval[maxlength]) NOT NULL";
			}
			
			
		}
		$sqltxt .= join(",\n",$fields);
	#	$sqltxt .= ",INDEX(".join(",",$keys)."))";
		$sqltxt .= ")";
		#echo $sqltxt;
		$this->db->queryF($sqltxt);
		echo $this->db->Error();
	}
	    function &get($id)
    {
        $id = intval($id);
        if ($id > 0) {
		
            $sql = 'SELECT * FROM '.$this->db->prefix($this->table).' WHERE id='.$id;
            if (!$result = $this->db->query($sql)) {
                return false;
            }
            $numrows = $this->db->getRowsNum($result);
            if ($numrows == 1) {
		#		echo $this->$objname;
                $item = new neogeoidx_city();
                $item->assignVars($this->db->fetchArray($result));
                return $item;
            }
        }
        return false;
    }

	function insert(&$item){
	  if (!$item->cleanVars()) {
            return false;
        }
	 $fields = array();
	 $values = array();
	  foreach ($item->cleanVars as $k => $v) {
       $fields[]=$k;            
	  switch($item->vars[$k]['data_type']){
	 
	  case XOBJ_DTYPE_INT:
			$values[] =  $v;
			break;
	  case XOBJ_DTYPE_TXTBOX:
			$values[] =  "'$v'";		
	  }
	 
	 }
	 $sqltxt = "INSERT INTO ".$this->db->prefix($this->table)." (".join(',',$fields).") VALUES (".join(",",$values).")";
	# echo $sqltxt;
	 $this->db->queryF($sqltxt);
	 echo $this->db->Error();
	}
	
	function indexing(){
		global $xoopsDB;
;
	$countryH = & xoops_getmodulehandler('country','neogeo');
	$regionH = & xoops_getmodulehandler('region','neogeo');
	$cityH = & xoops_getmodulehandler('city','neogeo');

	$indexO = $this->create();
	$this->createtable($indexO);
	$countries = $countryH->getObjects(null,1);
	$regions = $regionH->getObjects(null,1);
	$cityres = $cityH->getObjects(null,0,true);

	while ($cityrow = $xoopsDB->fetchArray($cityres)) {
		$indexO->setVar('id',$cityrow['id']);
		$indexO->setVar('city_id',$cityrow['id']);
		$indexO->setVar('city_title',$cityrow['title']);		
		$regionO=$regions[$cityrow['regid']];
        if (is_object($regionO)){
		$indexO->setVar("region_id",$regionO->getVar("id"));
		$indexO->setVar("region_title",$regionO->getVar("title"));		
		$countryid = $regionO->getVar("countryid");

    		$countryO = $countries[$countryid];
    		if (is_object($countryO)){
    		$indexO->setVar("country_id",$countryO->getVar('id'));
    		$indexO->setVar("country_title",$countryO->getVar('title'));		
    		}
		}
		$this->insert($indexO);
	}

	
	
	
	/*for ($i=1;$i<=99;$i++){
		$indexO->setVar('obj_id',$i);
		$this->insert($indexO);
	}*/
	}
    function &getObjects($criteria = null, $id_as_key = false, $is_res=false)
    {
        $ret = array();
        $limit = $start = 0;
            $sql = 'SELECT * FROM '.$this->db->prefix($this->table)." ";
     
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
            
			$sql .= $criteria->renderWhere();
			$sort =  $criteria->getSort();
            $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
            $limit = $criteria->getLimit();
            $start = $criteria->getStart();
        }
	#echo $sql;
        $result = $this->db->query($sql, $limit, $start);
        if (!$result) {
            return $ret;
        }
		if ($is_res) return $result;
        while ($myrow = $this->db->fetchArray($result)) {
            $item = new neogeoidx_city();
            $item->assignVars($myrow);
            if (!$id_as_key) {
                $ret[] =& $item;
            } else {
                $ret[$myrow['id']] =& $item;
            }
            unset($item);
        }
        return $ret;
    }

    /**
     * Count some images
     * 
     * @param   object  $criteria   {@link CriteriaElement} 
     * @return  int
     **/
    function getCount($criteria = null)
    {
        $limit = $start = 0;
            $sql = 'SELECT * FROM '.$this->db->prefix($this->table)." ";
     
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
            
			$sql .= $criteria->renderWhere();

        }


        if (!$result =& $this->db->query($sql)) {
            return 0;
        }
        list($count) = $this->db->fetchRow($result);
        return $count;
    }

	
}

?>